{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Admin where

import Import
import System.Cmd (rawSystem)

getAdminR :: Handler RepHtml
getAdminR = do
  ((resultUserForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
  defaultLayout $ do
    setTitle "Please authentificate"
    $(widgetFile "adminform")

postAdminR :: Handler RepHtml
postAdminR = do
  ((resultAdminForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
  (_) <- case resultAdminForm of
    FormSuccess res -> do
      checkAndDirectAdmin (fst res) (snd res)
    _               -> redirect AdminR
  defaultLayout $ do
    setTitle "Please authentificate"
    $(widgetFile "adminform")

getAdminMainR :: Handler RepHtml
getAdminMainR = do
  sess <- lookupSession "AdminAuthed"
  case sess of
    Just _  -> do
      defaultLayout $ do
        setTitle "SDDPV1 Admin Page"
        $(widgetFile "adminmain")
    Nothing -> redirect AdminR

getAdminAddR :: Handler RepHtml
getAdminAddR = do
  sess <- lookupSession "AdminAuthed"
  case sess of
    Just _  -> do
      ((resultAdminForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
      defaultLayout $ do
        setTitle "SDDPV1 Admin Add Page"
        $(widgetFile "adminaddform")
    Nothing -> redirect AdminR

postAdminAddR :: Handler RepHtml
postAdminAddR = do
  ((resultAdminForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
  (_) <- case resultAdminForm of
    FormSuccess res -> do
      let name     = fst res
          password = snd res
      curUsed <- runDB $ getBy $ UniqueName name
      case curUsed of
        Just (Entity _ used) -> do
          if not (usedStatus used)
            then do
            runDB $ do
              deleteBy $ UniqueName name
              (_) <- insert $ Used name password True
              return ()
            redirect AdminMainR
            else redirect AdminMainR
        _                    -> do
          (_) <- runDB $ insert $ Used name password True
          redirect AdminMainR
    _               -> redirect AdminMainR
  defaultLayout $ do
    setTitle "SDDPV1 Admin Add Page"
    $(widgetFile "adminaddform")

getAdminDeleteR :: Handler RepHtml
getAdminDeleteR = do
  sess <- lookupSession "AdminAuthed"
  case sess of
    Just _  -> do
      ((resultAdminForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
      defaultLayout $ do
        setTitle "SDDPV1 Admin Delete User"
        $(widgetFile "admindeleteform")
    Nothing -> redirect AdminR

postAdminDeleteR :: Handler RepHtml
postAdminDeleteR = do
  ((resultAdminForm, adminFormWidget), adminFormEnctype) <- runFormPost adminForm
  (_) <- case resultAdminForm of
    FormSuccess res -> do
      let name = fst res
      curUsed <- runDB $ getBy $ UniqueName name
      case curUsed of
        Just (Entity _ used) -> do
          runDB $ do
            deleteBy $ UniqueName name
            (_) <- insert $ Used name (snd res) False
            return ()
          redirect AdminMainR
        _         -> redirect AdminMainR
    _               -> redirect AdminMainR
  defaultLayout $ do
    setTitle "SDDPV1 Admin Delete User"
    $(widgetFile "admindeleteform")

checkAndDirectAdmin :: RedirectUrl master (Route App) => Text -> Text -> GHandler sub master a
checkAndDirectAdmin name password =
  if name == "admin" && password == "password"
  then do
    setSession "AdminAuthed" "yes"
    redirect AdminMainR
  else redirect AdminR

adminForm :: Form (Text, Text)
adminForm =
  renderDivs $ (,)
  <$> areq textField "User"     Nothing
  <*> areq textField "Password" Nothing